package com.qf.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@Controller
public class UploadController {

    @RequestMapping("/upload")
    public String upload(String username, MultipartFile file, HttpSession session){
        System.out.println("username: " + username);

        //动态获取服务器路径
        String realPath = session.getServletContext().getRealPath("/upload");
        System.out.println(realPath);
        //如果此路径不存在，则创建此路径
        if(!new File(realPath).exists()){
            new File(realPath).mkdirs();
        }


        //文件的原始名称  8.jpg
        String originalFilename = file.getOriginalFilename();
        String uuid = UUID.randomUUID().toString();


        try {
            file.transferTo(new File(realPath+"/" +uuid + originalFilename));
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "hello";
    }

}
